home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.11 Nov 95 / ODF Beeper / Sources / Actions.cpp next >
Encoding:
Text File  |  1995-09-26  |  11.2 KB  |  427 lines  |  [TEXT/MPCC]

  1. //==========================================================
  2. //
  3. //    File:                Actions.h
  4. //    Release Version:    $ 1.0d9 $
  5. //
  6. //    Author:                Jim Lloyd
  7. //
  8. //    Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //==========================================================
  11.  
  12. #ifndef ACTIONS_H
  13. #include "Actions.h"
  14. #endif
  15.  
  16. #ifndef BEEPERDEF_H
  17. #include "BeeperDef.h"
  18. #endif
  19.  
  20. // ----- OS Layer -----
  21.  
  22. #ifndef FWBARRAY_H
  23. #include "FWBArray.h"
  24. #endif
  25.  
  26. #ifndef FWMEMMGR_H
  27. #include "FWMemMgr.h"
  28. #endif
  29.  
  30. // ----- OpenDoc Includes -----
  31.  
  32. #ifndef _ODTYPES_
  33. #include <ODTypes.h>
  34. #endif
  35.  
  36. #ifndef SOM_Module_OpenDoc_StdProps_defined
  37. #include <StdProps.xh>
  38. #endif
  39.  
  40. #ifndef SOM_ODStorageUnit_xh
  41. #include <StorageU.xh>
  42. #endif
  43.  
  44. #ifndef SOM_ODTranslation_xh
  45. #include <Translt.xh>
  46. #endif
  47.  
  48. #ifndef SOM_ODSession_xh
  49. #include <ODSessn.xh>
  50. #endif
  51.  
  52. #ifndef SOM_ODDragItemIterator_xh
  53. #include <DgItmIt.xh>
  54. #endif
  55.  
  56. // ----- Macintosh Includes -----
  57.  
  58. #if defined(FW_BUILD_MAC) && !defined(__DRAG__)
  59. #include <Drag.h>
  60. #endif
  61.  
  62. #if defined(FW_BUILD_MAC) && !defined(__SOUND__)
  63. #include <Sound.h>
  64. #endif
  65.  
  66. #ifndef __OSA__
  67. #include <OSA.h>
  68. #endif
  69.  
  70. //==========================================================
  71. //    class CAction
  72. //==========================================================
  73.  
  74. //----------------------------------------------------------
  75. //    CAction::CAction
  76. //----------------------------------------------------------
  77.  
  78. CAction::CAction()
  79. {
  80. }
  81.  
  82. //----------------------------------------------------------
  83. //    CAction::~CAction
  84. //----------------------------------------------------------
  85.  
  86. CAction::~CAction()
  87. {
  88. }
  89.  
  90. //==========================================================
  91. //    class CSoundAction
  92. //==========================================================
  93.  
  94. //----------------------------------------------------------
  95. //    CSoundAction::CSoundAction
  96. //----------------------------------------------------------
  97.  
  98. CSoundAction::CSoundAction()
  99. {
  100. }
  101.  
  102. //----------------------------------------------------------
  103. //    CSoundAction::~CSoundAction
  104. //----------------------------------------------------------
  105.  
  106. CSoundAction::~CSoundAction()
  107. {
  108. }
  109.  
  110. //----------------------------------------------------------
  111. //    CSoundAction::IsInStorage
  112. //----------------------------------------------------------
  113.  
  114. FW_Boolean CSoundAction::IsInStorage(Environment* ev, ODStorageUnit* storage)
  115. {
  116.     FW_Boolean result = false;
  117.     if (storage->Exists(ev, kODPropContents, kSoundScrapKind, 0))        // 'snd ' on Scrap
  118.         result = true;
  119.     else if (storage->Exists(ev, kODPropContents, kSoundFileKind, 0))    // sound file
  120.         result = true;
  121.     return result;
  122. }
  123.  
  124. //----------------------------------------------------------
  125. //    CSoundAction::Internalize
  126. //----------------------------------------------------------
  127.  
  128. void CSoundAction::Internalize(Environment* ev, 
  129.                                 ODStorageUnit* storage)
  130. {
  131.     FW_Boolean internalized = FALSE;
  132.     
  133.     if (storage->Exists(ev, 
  134.                     kODPropContents, kSoundScrapKind, 0))
  135.     {
  136.         // Mac 'snd ' in Scrap
  137.         storage->Focus(ev, 
  138.                         kODPropContents, 
  139.                         kODPosUndefined, 
  140.                         kSoundScrapKind, 
  141.                         0,
  142.                         kODPosUndefined);
  143.         InternalizeSound(ev, storage);
  144.     }
  145.     else if (storage->Exists(ev, 
  146.                     kODPropContents, kSoundFileKind, 0))    
  147.     {
  148.         // Mac sound file
  149.         storage->Focus(ev, 
  150.                         kODPropContents, 
  151.                         kODPosUndefined, 
  152.                         kHFSFlavorType, 
  153.                         0, 
  154.                         kODPosUndefined);
  155.         InternalizeSoundFile(ev, storage);
  156.     }
  157. }
  158.  
  159. //----------------------------------------------------------
  160. //    CSoundAction::InternalizeSound
  161. //----------------------------------------------------------
  162.  
  163. void CSoundAction::InternalizeSound(Environment* ev, 
  164.                                 ODStorageUnit* storage)
  165. {
  166.     // Assume the storage unit is already focused
  167.     unsigned long size = storage->GetSize(ev);
  168.     if (size > 0)
  169.     {
  170.         FW_CAcquireTemporarySystemHandle handle(size);
  171.         FW_CByteArray byteArray;
  172.         storage->GetValue(ev, size, byteArray);
  173.         byteArray.CopyBuffer(handle.GetPointer(), size);
  174.         fSoundHandle = handle.GetPlatformHandle();
  175.         handle.Orphan();
  176.     }
  177. }
  178.  
  179. //----------------------------------------------------------
  180. //    CSoundAction::InternalizeSoundFile
  181. //----------------------------------------------------------
  182.  
  183. void CSoundAction::InternalizeSoundFile(Environment* ev, 
  184.                                     ODStorageUnit* storage)
  185. {
  186.     // StorageUnit should already be Focused on HFSFlavor
  187.     unsigned long hfsSize = storage->GetSize(ev);
  188.     if (hfsSize > 0)
  189.     {
  190.         // Get the HFS flavor
  191.         HFSFlavor hfsInfo;
  192.         FW_CByteArray byteArray;
  193.         storage->GetValue(ev, sizeof(HFSFlavor), byteArray);
  194.         byteArray.CopyBuffer(&hfsInfo, sizeof(HFSFlavor));
  195.         
  196.         // Create platform independent file spec
  197.         FW_CFileSpecification fileSpec(hfsInfo.fileSpec);
  198.         
  199.         // Open the resource file
  200.         FW_CResourceFile resourceFile(fileSpec);
  201.         
  202.         // Get any (i.e the first) sound resource
  203.         const FW_ResourceType resourceType = 'snd ';
  204.         const FW_ResourceId resourceId = 1;
  205.         Handle sound = ::Get1IndResource(resourceType, 
  206.                                         resourceId);
  207.                                         
  208.         if (sound != NULL)
  209.         {
  210.             // if succeeded, detach the handle and keep it
  211.             ::DetachResource(sound);
  212.             fSoundHandle = sound;
  213.         }
  214.         else
  215.         {
  216.             // No sound resource loaded, throw an exception
  217.             FW_THROW(FW_XResourceNotFound(resourceFile,
  218.                                             resourceId, 
  219.                                             resourceType));
  220.         }
  221.     }
  222. }
  223.  
  224. //----------------------------------------------------------
  225. //    CSoundAction::Externalize
  226. //----------------------------------------------------------
  227.  
  228. void CSoundAction::Externalize(Environment* ev, 
  229.                                 ODStorageUnit* storage)
  230. {
  231.     // Assume we're focused on the correct property
  232.     storage->AddValue(ev, kSoundScrapKind);
  233.     if (fSoundHandle != NULL)
  234.     {
  235.         FW_CAcquireLockedSystemHandle lock(fSoundHandle);
  236.         FW_CByteArray byteArray(*fSoundHandle, 
  237.                     FW_CMemoryManager::
  238.                     GetSystemHandleSize(fSoundHandle));
  239.         storage->SetValue(ev, byteArray);
  240.     }
  241. }
  242.  
  243. //----------------------------------------------------------
  244. // CSoundAction::DoIt
  245. //----------------------------------------------------------
  246.  
  247. void CSoundAction::DoIt()
  248. {
  249.     if (fSoundHandle != NULL)
  250.     {
  251.         FW_CAcquireLockedSystemHandle lock(fSoundHandle);
  252.         ::SndPlay(NULL, (SndListHandle)fSoundHandle, TRUE);
  253.     }
  254. }
  255.  
  256. //==========================================================
  257. //    class CScriptAction
  258. //==========================================================
  259.  
  260. //----------------------------------------------------------
  261. //    CScriptAction::CScriptAction
  262. //----------------------------------------------------------
  263.  
  264. CScriptAction::CScriptAction()
  265. {
  266. }
  267.  
  268. //----------------------------------------------------------
  269. //    CScriptAction::~CScriptAction
  270. //----------------------------------------------------------
  271.  
  272. CScriptAction::~CScriptAction()
  273. {
  274. }
  275.  
  276. //----------------------------------------------------------
  277. //    CScriptAction::IsInStorage
  278. //----------------------------------------------------------
  279.  
  280. FW_Boolean CScriptAction::IsInStorage(Environment* ev, ODStorageUnit* storage)
  281. {
  282.     FW_Boolean result = false;
  283.     if (storage->Exists(ev, kODPropContents, kScriptScrapKind, 0))        // 'scpt' on Scrap
  284.         result = true;
  285.     else if (storage->Exists(ev, kODPropContents, kScriptFileKind, 0))    // compiled script file
  286.         result = true;
  287.     return result;
  288. }
  289.  
  290. //----------------------------------------------------------
  291. //    CScriptAction::Internalize
  292. //----------------------------------------------------------
  293.  
  294. void CScriptAction::Internalize(Environment* ev, ODStorageUnit* storage)
  295. {
  296.     FW_Boolean internalized = FALSE;
  297.     
  298.     if (storage->Exists(ev, kODPropContents, kScriptScrapKind, 0))    // 'scpt' on Scrap
  299.     {
  300.         storage->Focus(ev, kODPropContents, kODPosUndefined, kScriptScrapKind, 0, kODPosUndefined);
  301.         InternalizeScript(ev, storage);
  302.     }
  303.     else if (storage->Exists(ev, kODPropContents, kScriptFileKind, 0))    // compiled script file
  304.     {
  305.         storage->Focus(ev, kODPropContents, kODPosUndefined, kHFSFlavorType, 0, kODPosUndefined);
  306.         InternalizeScriptFile(ev, storage);
  307.     }
  308. }
  309.  
  310. //----------------------------------------------------------
  311. //    CScriptAction::InternalizeScript
  312. //----------------------------------------------------------
  313.  
  314. void CScriptAction::InternalizeScript(Environment* ev, ODStorageUnit* storage)
  315. {
  316.     // Assume the storage unit is already focused
  317.     unsigned long size = storage->GetSize(ev);
  318.     if (size > 0)
  319.     {
  320.         FW_CAcquireTemporarySystemHandle handle(size);
  321.         FW_CByteArray byteArray;
  322.         storage->GetValue(ev, size, byteArray);
  323.         byteArray.CopyBuffer(handle.GetPointer(), size);
  324.         fScriptHandle = handle.GetPlatformHandle();
  325.         handle.Orphan();
  326.     }
  327. }
  328.  
  329. //----------------------------------------------------------
  330. //    CScriptAction::InternalizeScriptFile
  331. //----------------------------------------------------------
  332.  
  333. void CScriptAction::InternalizeScriptFile(Environment* ev, 
  334.                                     ODStorageUnit* storage)
  335. {
  336.     // StorageUnit should already be Focused on HFSFlavor
  337.     unsigned long hfsSize = storage->GetSize(ev);
  338.     if (hfsSize > 0)
  339.     {
  340.         // Get the HFS flavor
  341.         HFSFlavor hfsInfo;
  342.         FW_CByteArray byteArray;
  343.         storage->GetValue(ev, sizeof(HFSFlavor), byteArray);
  344.         byteArray.CopyBuffer(&hfsInfo, sizeof(HFSFlavor));
  345.         
  346.         // Make the file specification
  347.         FW_CFileSpecification fileSpec(hfsInfo.fileSpec);
  348.         
  349.         // Acquire an opened resource file, 
  350.         // will be closed automatically
  351.         FW_CResourceFile resourceFile(fileSpec);
  352.         
  353.         // Acquire an opened resource, 
  354.         // will be released automatically
  355.         FW_CResource resource(resourceFile, 128, 'scpt');
  356.         
  357.         // Copy the resource handle
  358.         fScriptHandle = FW_CMemoryManager::
  359.                     CopySystemHandle(resource.GetHandle());
  360.     }
  361. }
  362.  
  363. //----------------------------------------------------------
  364. //    CScriptAction::Externalize
  365. //----------------------------------------------------------
  366.  
  367. void CScriptAction::Externalize(Environment* ev, ODStorageUnit* storage)
  368. {
  369.     storage->AddValue(ev, kScriptScrapKind);
  370.     if (fScriptHandle != NULL)
  371.     {
  372.         // Acquire a lock on the system handle, will be unlocked automatically
  373.         FW_CAcquireLockedSystemHandle lock(fScriptHandle);
  374.         
  375.         // Copy the data into a SOM compatible object, and then write it to the storage unit
  376.         FW_CByteArray byteArray(lock.GetPointer(), FW_CMemoryManager::GetSystemHandleSize(fScriptHandle));
  377.         storage->SetValue(ev, byteArray);
  378.     }
  379. }
  380.  
  381. //----------------------------------------------------------
  382. //    LoadAndExecuteScript
  383. //----------------------------------------------------------
  384. // This function was lifted almost verbatim from
  385. // IM:IAC, page 10-16. (Actually, I used MPTA, but it says 
  386. // the code is on the above page)
  387.  
  388. void CScriptAction::LoadAndExecuteScript(Handle scriptData)
  389. {
  390.     AEDesc scriptDesc;
  391.     OSAID scriptID, resultID;
  392.     AEDesc scriptText;
  393.     OSAError error;
  394.     static ComponentInstance gComponent = 0;
  395.  
  396.     if (!gComponent)
  397.         gComponent = ::OpenDefaultComponent(
  398.                     kOSAComponentType, 
  399.                     kOSAGenericScriptingComponentSubtype);
  400.  
  401.     // load the script data
  402.     scriptDesc.descriptorType = typeOSAGenericStorage;
  403.     scriptDesc.dataHandle = scriptData;
  404.     error = ::OSALoad(gComponent, &scriptDesc, 
  405.                             kOSAModeNull, &scriptID);
  406.     
  407.     if (error == noErr)
  408.     { 
  409.         // execute the compiled script in the default context                 
  410.         error = ::OSAExecute(gComponent, 
  411.                         scriptID, kOSANullScript, 
  412.                         kOSAModeNull, &resultID);
  413.         error = ::OSADispose(gComponent, scriptID);
  414.         error = ::OSADispose(gComponent, resultID);
  415.     }
  416. }
  417.  
  418. //----------------------------------------------------------
  419. // CScriptAction::DoIt
  420. //----------------------------------------------------------
  421.  
  422. void CScriptAction::DoIt()
  423. {
  424.     if (fScriptHandle != NULL)
  425.         LoadAndExecuteScript(fScriptHandle);
  426. }
  427.